//
//  SDAnswerTestData.m
//
//  Created by   on 2019/3/11
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SDAnswerTestData.h"
#import "SDAnswerTestSubjectList.h"
#import "SDAnswerTestChildList.h"


NSString *const kSDAnswerTestDataSubjectList = @"subjectList";
NSString *const kSDAnswerTestDataId = @"id";
NSString *const kSDAnswerTestDataTitle = @"title";
NSString *const kSDAnswerTestDataChildList = @"childList";
NSString *const kSDAnswerTestDataDesc = @"desc";


@interface SDAnswerTestData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDAnswerTestData

@synthesize subjectList = _subjectList;
@synthesize id = _id;
@synthesize title = _title;
@synthesize childList = _childList;
@synthesize desc = _desc;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
    NSObject *receivedSDAnswerTestSubjectList = [dict objectForKey:kSDAnswerTestDataSubjectList];
    NSMutableArray *parsedSDAnswerTestSubjectList = [NSMutableArray array];
    
    if ([receivedSDAnswerTestSubjectList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDAnswerTestSubjectList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDAnswerTestSubjectList addObject:[SDAnswerTestSubjectList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDAnswerTestSubjectList isKindOfClass:[NSDictionary class]]) {
       [parsedSDAnswerTestSubjectList addObject:[SDAnswerTestSubjectList modelObjectWithDictionary:(NSDictionary *)receivedSDAnswerTestSubjectList]];
    }

    self.subjectList = [NSArray arrayWithArray:parsedSDAnswerTestSubjectList];
            self.id = [[self objectOrNilForKey:kSDAnswerTestDataId fromDictionary:dict] doubleValue];
            self.title = [self objectOrNilForKey:kSDAnswerTestDataTitle fromDictionary:dict];
    NSObject *receivedSDAnswerTestChildList = [dict objectForKey:kSDAnswerTestDataChildList];
    NSMutableArray *parsedSDAnswerTestChildList = [NSMutableArray array];
    
    if ([receivedSDAnswerTestChildList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDAnswerTestChildList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDAnswerTestChildList addObject:[SDAnswerTestChildList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDAnswerTestChildList isKindOfClass:[NSDictionary class]]) {
       [parsedSDAnswerTestChildList addObject:[SDAnswerTestChildList modelObjectWithDictionary:(NSDictionary *)receivedSDAnswerTestChildList]];
    }

    self.childList = [NSArray arrayWithArray:parsedSDAnswerTestChildList];
            self.desc = [self objectOrNilForKey:kSDAnswerTestDataDesc fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForSubjectList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.subjectList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForSubjectList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForSubjectList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForSubjectList] forKey:kSDAnswerTestDataSubjectList];
    [mutableDict setValue:[NSNumber numberWithDouble:self.id] forKey:kSDAnswerTestDataId];
    [mutableDict setValue:self.title forKey:kSDAnswerTestDataTitle];
    NSMutableArray *tempArrayForChildList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.childList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForChildList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForChildList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForChildList] forKey:kSDAnswerTestDataChildList];
    [mutableDict setValue:self.desc forKey:kSDAnswerTestDataDesc];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.subjectList = [aDecoder decodeObjectForKey:kSDAnswerTestDataSubjectList];
    self.id = [aDecoder decodeDoubleForKey:kSDAnswerTestDataId];
    self.title = [aDecoder decodeObjectForKey:kSDAnswerTestDataTitle];
    self.childList = [aDecoder decodeObjectForKey:kSDAnswerTestDataChildList];
    self.desc = [aDecoder decodeObjectForKey:kSDAnswerTestDataDesc];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_subjectList forKey:kSDAnswerTestDataSubjectList];
    [aCoder encodeDouble:_id forKey:kSDAnswerTestDataId];
    [aCoder encodeObject:_title forKey:kSDAnswerTestDataTitle];
    [aCoder encodeObject:_childList forKey:kSDAnswerTestDataChildList];
    [aCoder encodeObject:_desc forKey:kSDAnswerTestDataDesc];
}

- (id)copyWithZone:(NSZone *)zone {
    SDAnswerTestData *copy = [[SDAnswerTestData alloc] init];
    
    
    
    if (copy) {

        copy.subjectList = [self.subjectList copyWithZone:zone];
        copy.id = self.id;
        copy.title = [self.title copyWithZone:zone];
        copy.childList = [self.childList copyWithZone:zone];
        copy.desc = [self.desc copyWithZone:zone];
    }
    
    return copy;
}


@end
